package com.cuiweiyou.contactsfromfile;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

/**
 */

public class PermissionTool {
    /**
     * 跳转到miui的权限管理页面
     */
    private static void gotoMiuiPermission(Context context) {
        try {
            Intent i = new Intent("miui.intent.action.APP_PERM_EDITOR");
            ComponentName componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
            i.setComponent(componentName);
            i.putExtra("extra_pkgname", context.getPackageName());

            context.startActivity(i);
        } catch (Exception e) {
            try {
                Intent i = new Intent("miui.intent.action.APP_PERM_EDITOR");
                ComponentName componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
                i.setComponent(componentName);
                i.putExtra("extra_pkgname", context.getPackageName());

                context.startActivity(i);
            } catch (Exception e1) {
                e1.printStackTrace();
                startAppSettingActivity(context);
            }
        }
    }


    /**
     * 跳转到魅族的权限管理系统
     */
    private static void gotoMeizuPermission(Context context) {
        Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.putExtra("packageName", context.getPackageName());
        try {
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            startAppSettingActivity(context);
        }
    }

    /**
     * 华为的权限管理页面
     */
    private static void gotoHuaweiPermission(Context context) {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");//华为权限管理
            intent.setComponent(comp);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            startAppSettingActivity(context);
        }
    }


    /**
     * vivo的权限管理页面
     */
    private static void gotoVivoPermission(Context context) {
        try {// com.vivo.permissionmanager/.activity.PurviewTabActivity //权限设置页面
            // com.vivo.permissionmanager/.activity.SoftPermissionDetailActivity // 单个应用的权限设置页面
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.PurviewTabActivity");//vivo X9L权限管理
            intent.setComponent(comp);
            context.startActivity(intent);
        } catch (Exception e) {
            try {
                Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.iqoo.secure");
                if (intent != null) {
                    context.startActivity(intent);
                } else {
                    startAppSettingActivity(context);
                }
                // java.lang.SecurityException: Permission Denial: starting Intent { flg=0x10000000 cmp=com.iqoo.secure/.safeguard.PurviewTabActivity (has extras) } from ProcessRecord{17bbc0e7 28905:com.weidai.loan/u0a161} (pid=28905, uid=10161) not exported from uid 10052
                //            Intent intent = new Intent();
                //            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //            ComponentName comp = new ComponentName("com.iqoo.secure", "com.iqoo.secure.safeguard.PurviewTabActivity");//vivo权限管理
                //            intent.setComponent(comp);
                //            context.startActivity(intent);
            } catch (Exception e1) {
                e1.printStackTrace();
                startAppSettingActivity(context);
            }
        }

    }

    /**
     * oppo的权限管理页面
     */
    private static void gotoOppoPermission(Context context) {
        try {
            Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.oppo.safe");
            if (intent != null) {
                context.startActivity(intent);
            } else {
                startAppSettingActivity(context);
            }
        } catch (Exception e) {
            e.printStackTrace();
            startAppSettingActivity(context);
        }
    }

    /**
     * 360的权限管理页面
     */
    private static void goto360Permission(Context context) {
        try {
            // com.qiku.android.security/.ui.activity.AppSecurityMainListActivity
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("com.qiku.android.security", "com.qiku.android.security.ui.activity.AppSecurityMainListActivity");
            intent.setComponent(comp);
            context.startActivity(intent);
        } catch (Exception e) {
            startAppSettingActivity(context);
        }

    }

    /**
     * 获取应用详情页面intent
     *
     * @return
     */
    private static Intent getAppDetailSettingIntent(Context context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            //            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        return localIntent;
    }

    private static void startAppSettingActivity(Context context) {
        context.startActivity(getAppDetailSettingIntent(context));
    }

    public static void startAppPermissionSettingActivity(Context context) {
        if (Build.MANUFACTURER.equalsIgnoreCase("xiaomi")) {
            gotoMiuiPermission(context);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("huawei")) {
            gotoHuaweiPermission(context);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("meizu")) {
            gotoMeizuPermission(context);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("vivo")) {
            gotoVivoPermission(context);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("oppo")) {
            gotoOppoPermission(context);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("360")) {
            goto360Permission(context);
        } else {
            startAppSettingActivity(context);
        }
    }

    public static void showPermissionRejectedDialog(final Activity activity) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("无授权");                           // 标题
        builder.setMessage("没有权限继续进行，退出应用");   // 信息

        // 确定按钮
        builder.setPositiveButton("退出", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                activity.finish();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }

    public static void showPermissionDialog(Activity activity, final OnNeedPermissionAgreeBack back) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("请授权");                           // 标题
        builder.setMessage("须要权限进行操作");   // 信息

        // 确定按钮
        builder.setPositiveButton("授权", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                back.agree(true);
            }
        });

        // 取消按钮
        builder.setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                back.agree(false);
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }

    public interface OnNeedPermissionAgreeBack {
        void agree(boolean authorized);
    }

    public static void requestPermissionFromSettingDialog(final Activity activity) {

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("请授权");
        builder.setMessage("缺少必要的权限，请去设置界面打开");

        // 确定按钮
        builder.setPositiveButton("前往设置", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); // 本应用的设置页
                intent.setData(Uri.parse("package:" + activity.getPackageName()));
                activity.startActivityForResult(intent, Constant.NUMBER_11);
            }
        });

        // 取消按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                activity.finish();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }
}

